package ca.abubakrsagar.breadboardsim;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JComponent;

public class MainSwitch extends JComponent {

	private Breadboard breadBoard; // The breadboard component.
	private boolean isOn = false; // The state of the switch.
	private MainSwitchController controller = new MainSwitchController(this); // The
																				// switch's
																				// controller.

	/**
	 * Creates a new mainSwitch while associating with the given breadboard and
	 * add a mouse listener.
	 * 
	 * @param breadBoard
	 *            The breadboard.
	 */
	public MainSwitch(Breadboard breadBoard) {
		this.breadBoard = breadBoard;
		this.addMouseListener(controller);
	}

	/**
	 * @return The state of the main switch.
	 */
	public boolean isOn() {
		return isOn;
	}

	/**
	 * Set the main switches state to the given value.
	 * 
	 * @param isOn
	 *            The state of the main switch.
	 */
	public void setOn(boolean isOn) {
		this.isOn = isOn;
		if (isOn) {				//sets the volts of the top two rows and the bottom two
			for (int i = 0; i < 2; i++) {
				for (int j = 0; j < 64; j++) {
					breadBoard.setVolts(i, j, 6.0);
				}
			}
			for (int i = 12; i < 14; i++) {
				for (int j = 0; j < 64; j++) {
					breadBoard.setVolts(i, j, 6.0);
				}
			}
		}
		repaint();
	}

	/**
	 * Draws the main switch based on its state.
	 */
	public void paintComponent(Graphics g) {
		//draws the switch
		Graphics2D g2 = (Graphics2D) g;
		g2.setColor(new Color(189, 189, 189));
		g2.fillRect(0, 0, 50, 20);

		if (isOn) {
			g2.setColor(new Color(18, 199, 63));
			g2.fillRect(28, 5, 17, 10);
		} else {
			g2.setColor(new Color(245, 10, 53));
			g2.fillRect(5, 5, 17, 10);
		}
	}

}
